package org.example.demo.broker;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.*;


public class MessageHolderAsyncQueue {
    private static final Logger LOGGER = LoggerFactory.getLogger(MessageHolderAsyncQueue.class);

    private static final int CORE_SIZE = Runtime.getRuntime().availableProcessors();
    private static final int QUEUE_SIZE = 10000;

    private static final ExecutorService SENDER_ASYNC = new ThreadPoolExecutor(
            CORE_SIZE,
            CORE_SIZE,
            60L,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(QUEUE_SIZE),
            r -> {
                Thread t = new Thread(r);
                t.setName("message_holder_async_queue");
                return t;
            },
            (r, executor) -> LOGGER.error("async sender is error rejected, runnable: {}, executor: {}", r, executor)
    );

    public static void submit(Runnable runnable) {
        SENDER_ASYNC.submit(runnable);
    }
}
